<template>
  <div class="choose-enterprise">
    <div class="input-container" @click="openDialog">
      <el-tag
          v-if="ent"
          :key="ent.serialNo"
          class="diy-tag">
        {{ ent.enterpriseName }}
      </el-tag>
    </div>

    <el-dialog ref="dialog" title="选择组织" show-close modal :visible.sync="open" width="1100px" append-to-body>
      <div class="app-container">
        <lst-page-container>
          <template slot="query">
            <el-form :model="tableQueryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
              <!--              <el-form-item v-if="showQuery('deptCode')" label="所属组织" prop="deptCode">-->
              <!--                <el-cascader-->
              <!--                    clearable-->
              <!--                    :props="{value:'orgCode',label:'orgName',checkStrictly: true }"-->
              <!--                    v-model="tableQueryParams.deptCode"-->
              <!--                    :options="options"></el-cascader>-->
              <!--              </el-form-item>-->
              <el-form-item v-if="showQuery('enterpriseName')" label="企业名称" prop="enterpriseName">
                <el-input
                    v-model="tableQueryParams.enterpriseName"
                    placeholder="请输入企业名称"
                    clearable
                    style="width: 240px"
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </template>

          <template slot="table">
            <el-table class="single-choice" @select="handleSelectionChange" height="100%" v-loading="loading"
                      :data="dataList"
                      ref="defectTable">
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="企业类型" align="center" prop="enterpriseTypeName"/>
              <el-table-column label="企业名称" align="center" prop="enterpriseName"/>
            </el-table>
          </template>

          <template slot="pageDisplay">
            <pagination
                v-show="total>0"
                :total="total"
                :page.sync="tableQueryParams.pageNum"
                :limit.sync="tableQueryParams.pageSize"
                @pagination="getList"
            />
          </template>
        </lst-page-container>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPageMixins} from "@/mixins/listPageMixins";
import {isArr} from "@/utils/arrayUtil";
import {getDeptTree} from "@/api/system/org";
import {questionStatistic} from "@/api/checkPage/quertion";
import {ENT_CONFIG, ENT_QUERY_CONFIG} from "@/views/components/ChooseEnterprise/config";
import {getInfoBySerial} from "@/api/system/sysEnterprise";

export default {
  mixins: [listPageMixins],
  props: {
    handelCode: {
      type: [String, Number]
    },
    inspectInfo: {
      type: Object, default: () => {
        return {}
      }
    },
    value: {
      type: [String, Number]
    }
  },
  model: {
    prop: 'value',
    event: 'change11'
  },
  data() {
    return {
      ent: null,
      bufferEnt: null,
      open: false,
      ENT_CONFIG,
      // 查询参数
      queryParams: {
        // 隶属行政机构
        deptCode: null,
        // 企业名称
        enterpriseName: null,
      },
      otherQueryParam: {
        inspectId: this.inspectInfo.id
      },
      options: [],
    }
  },
  watch: {
  },
  methods: {
    getEnt() {
      let {value} = this;
      let owner = this
      getInfoBySerial({serial: value}).then(res => {
        owner.ent = _.cloneDeep(res.data)
        owner.bufferEnt = _.cloneDeep(res.data)
      })
    },
    showQuery(fieldName) {
      let item = ENT_QUERY_CONFIG[fieldName]
      if (!item) return false;
      let {handelCode} = this
      return item.some(type => type == handelCode)
    },
    handleSelectionChange(val, currentRow) {
      this.$refs.defectTable.clearSelection();
      this.$refs.defectTable.toggleRowSelection(currentRow);
      this.bufferEnt = _.cloneDeep(currentRow)
    },
    openDialog() {
      this.open = true
    },
    cancel() {
      this.open = false
    },
    submitForm() {
      this.ent = _.cloneDeep(this.bufferEnt)
      this.$emit('change11', this.ent.serialNo)
      this.cancel()
    },
    getList() {
      this.startLoading('正在加载....')
      let owner = this
      let {otherQueryParam} = this
      let queryParam = otherQueryParam != null ? {...otherQueryParam, ...this.tableQueryParams} : this.tableQueryParams;
      // queryParam.deptCode = isArr(queryParam.deptCode) ? queryParam.deptCode[queryParam.deptCode?.length - 1] : 0
      questionStatistic(ENT_CONFIG[this.handelCode].queryUrl, queryParam).then(response => {
            owner.dataList = response.rows;
            owner.total = response.total;
          }
      ).finally(() => {
        owner.endLoading()
      });
    },
    changeOptions(options) {
      const newOptions = [];
      options.forEach(item => {
        let {orgName, orgCode, children} = item;
        const newItem = isArr(children) ? {orgName, orgCode, children} : {orgName, orgCode,};
        if (isArr(newItem.children)) {
          // 递归处理子节点
          newItem.children = this.changeOptions(newItem.children);
        }
        newOptions.push(newItem);
      });
      return newOptions;
    },
    getTreeData() {
      this.startLoading('正在加载....')
      let owner = this
      return new Promise((resolve, reject) => {
        getDeptTree().then(res => {
          owner.options = owner.changeOptions(res.data)
          resolve(owner.options)
        }).finally(() => {
          owner.endLoading()
        })
      })
    },
  },
  created() {
    let {value:val} = this
    if (val != null && val != '') {
      if (this.ent == null || (this.ent?.serialNo != null && this.ent?.serialNo != '' && this.ent?.serialNo != val)) {
        this.getEnt();
      }
    } else {
      this.ent = null;
    }
    this.getTreeData()
    this.getList()
  }
}
</script>

<style scoped>

</style>
